<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src='~@/assets/heima.png' />
        后台管理系统
      </div>
      <el-button
        type="info"
        @click="logout"
      > 退出 </el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="isCollapse ? '64px' : '200px'">
        <div
          class='toggle-button'
          @click='toggleCollapse'
        >|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color='#409eff'
          unique-opened
          :collapse='isCollapse'
          :collapse-transition='false'
          :defallut-active='activePath'
          router
        >
          <el-submenu
            v-for='obj in TAbList'
            :key='obj.id'
            :index="obj.id + ''"
          >
            <template slot="title">
              <i :class="iconObj[obj.id]"></i>
              <span>{{obj.authName}}</span>
            </template>
            <el-menu-item
              v-for='i in obj.children'
              :key='i.id'
              :index="'/' + i.path"
              @click="saveNavState('/' + i.path)"
            >
              <i class="el-icon-menu"></i>
              <span slot="title">{{i.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 主体区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script scoped>
import { asidesAPI } from '../../api/home'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {},
  // 组件状态值
  data () {
    return {
      // 是否折叠
      isCollapse: false,
      TAbList: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 被激活的链接地址
      activePath: ''
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 初始化渲染数据
    async getTabList () {
      try {
        const { data: res } = await asidesAPI()
        if (res.meta.status !== 200) return this.$message.error('获取数据失败！')
        this.TAbList = res.data
      } catch (err) {
        this.$message.error('获取数据失败！请稍后再试')
      }
    },
    // 点击退出回到登录页面并清掉本地存储
    logout () {
      window.sessionStorage.removeItem('token')
      this.$router.push({
        path: '/login'
      })
    }
  },
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.getTabList()
    this.activePath = window.sessionStorage.getItem('avtivePath')
  }
}
</script>

<style scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
}
.el-header .el-button {
  margin: 10px 0;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div img {
  margin: 0 15px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
